const express = require('express')
const connection =require('../mysql');
const router = express.Router();
const Response = require('../response');
router.post("/",function(req,res)  {
    let param = req.body;
    let username = param.username;
    let password = param.password;
    let phone = param.phone;
    let response = new Response(false,'',-1);
    if(username && password && phone){
        //1、查看数据库中是否有相同用户名
        connection.query("SELECT * FROM USER user username = ?",[username], function (error, results, fields) {
            if (error) throw error;
            if(results.length >= 1){
                //2、如果有相同用户名，则注册失败，用户名重复
                response = new Response(false,'注册失败，用户名重复',-1);
                //打印响应报文
                // logger.info(response);
                res.send(response);
            }else{
                connection.query("INSERT INTO user(username,password,phone) VALUES(?,?,?)", [username,password,phone],function (error, results, fields) {
                    if (error) throw error;
                    //3、如果没有相同用户名，并且有一条记录，则注册成功
                    if(results.affectedRows==1){
                        response = new Response(false,'注册成功',200);
                     
                        res.send(response);
                    }else {
                        response = new Response(false,'注册失败',-1);
                        // logger.info(response);
                        res.send(response);
                    }
                });
            }
        })
    }else{
        response = new Response(false,'注册失败，用户名、密码、用户号不能为空',-1);
        // logger.info(response);
        res.send(response);
    }
})

module.exports = router;